//
//  TestModule.m
//  WeexEros
//
//  Created by 侍晓明 on 2019/2/27.
//  Copyright © 2019年 benmu. All rights reserved.
//
#import "WaterMaskModule.h"
#import <WeexPluginLoader/WeexPluginLoader.h>

// 第一个参数为暴露给 js 端 Module 的名字，
// 第二个参数为你 Module 的类名
WX_PlUGIN_EXPORT_MODULE(waterMasker, WaterMaskModule)

@implementation WaterMaskModule

@synthesize weexInstance;

// 将方法暴露出去
WX_EXPORT_METHOD(@selector(test:))
WX_EXPORT_METHOD(@selector(transform:imgName:text:callback:))



// 实现 Module 方法
-  (void)transform:(NSString *)imgSrc imgName:(NSString *)imgName text:(NSString *)text callback:(WXModuleCallback)callback
{
    if(callback){
        NSDictionary *data = nil;
        NSString *path = [self addText:imgSrc :imgName :text];
        data = @{@"result": @"success",@"imgPath": path};
        callback(data);
    }
    
}

// 实现 Module 方法
-  (void)test:(WXModuleCallback)callback
{
    if(callback){

        
        NSDictionary *data = nil;
        data = @{@"result": @"success",@"imgPath": @"123"};
        callback(data);
    }
    
}



- (NSString *)addText:(NSString *)imgSrc :(NSString *)imgName :(NSString *)text {
    //获取本地图片
    NSString *filePath = imgSrc;
    NSData *image = [NSData dataWithContentsOfFile:filePath];
    UIImage *imgOrg =[UIImage imageWithData:image];
    
    //压缩
    UIImage *img = [self compressImageSize:imgOrg toByte:(300000)];
    
    //写入水印
    int w = img.size.width;
    int h = img.size.height;
    UIGraphicsBeginImageContext(img.size);
    [img drawInRect:CGRectMake(0, 0, w, h)];
    NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
    paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;
    NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:25],
                                NSParagraphStyleAttributeName: paragraphStyle,
                                NSForegroundColorAttributeName : [UIColor redColor],
//                                NSTextEffectAttributeName: NSTextEffectLetterpressStyle
                                };
    [text drawInRect:CGRectMake(0, 0, w/2 , h/2) withAttributes:attribute];
    UIImage *aImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    //缓存应用目录
    //缓存目录
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *path_cache = [paths objectAtIndex:0];
    //设置一个图片的存储路径
    imgName = [@"/" stringByAppendingString:imgName];
    NSString *imagePath = [path_cache stringByAppendingString:imgName];
    //把图片直接保存到指定的路径（同时应该把图片的路径imagePath存起来，下次就可以直接用来取）
    [UIImagePNGRepresentation(aImage) writeToFile:imagePath atomically:YES];
    
    
    return path_cache;
}

- (UIImage *)compressImageSize:(UIImage *)image toByte:(NSUInteger)maxLength {
    UIImage *resultImage = image;
    NSData *data = UIImageJPEGRepresentation(resultImage, 1);
    NSUInteger lastDataLength = 0;
    while (data.length > maxLength && data.length != lastDataLength) {
        lastDataLength = data.length;
        CGFloat ratio = (CGFloat)maxLength / data.length;
        CGSize size = CGSizeMake((NSUInteger)(resultImage.size.width * sqrtf(ratio)),
                                 (NSUInteger)(resultImage.size.height * sqrtf(ratio))); // Use NSUInteger to prevent white blank
        UIGraphicsBeginImageContext(size);
        // Use image to draw (drawInRect:), image is larger but more compression time
        // Use result image to draw, image is smaller but less compression time
        [resultImage drawInRect:CGRectMake(0, 0, size.width, size.height)];
        resultImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        data = UIImageJPEGRepresentation(resultImage, 1);
    }
    return resultImage;
}

@end

